package com.project.dao.impl;

import com.project.bean.FacilityBean;
import com.project.bean.RoomBean;
import com.project.dao.IFacilityDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@Repository
public class FacilityDaoImpl extends BaseDao implements IFacilityDao {


    @Override
    public void add(FacilityBean facility) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_facility(f_msg,f_time,f_state,fk_roomId) " +
                    "VALUES(?,?,?,?)");
            ps.setObject(1, facility.getMsg());
            ps.setObject(2, facility.getTime());
            ps.setObject(3, facility.getState());
            ps.setObject(4, facility.getRoomBean().getId());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }

    }

    @Override
    public List<FacilityBean> findByRoomId(int roomId) {

        List<FacilityBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_facility where fk_roomId=? ");
            ps.setObject(1, roomId);
            rs = ps.executeQuery();
            while (rs.next()) {
                FacilityBean facilityBean = new FacilityBean();
                facilityBean.setId(rs.getInt("pk_facilityId"));
                facilityBean.setMsg(rs.getString("f_msg"));
                facilityBean.setTime(LocalDate.parse(rs.getString("f_time")));
                facilityBean.setState(rs.getString("f_state"));
                list.add(facilityBean);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public void delByRoom(int roomId) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete  from t_facility where fk_roomId=? ");
            ps.setObject(1, roomId);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public void updateStatus(int id, String status) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_facility set f_state=? where pk_facilityId=? ");
            ps.setObject(1, status);
            ps.setObject(2, id);
            ps.executeUpdate();

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }

    }

    @Override
    public int findFacilitiesNum(int roomId, String status) {
        this.setConnection();
        int num=0;
        try {
            ps=con.prepareStatement("select count(*) c from t_facility where fk_roomId=? and f_state=? ");
            ps.setObject(1,roomId);
            ps.setObject(2,status);
           rs=ps.executeQuery();
           if (rs.next()){
             num=rs.getInt("c");
           }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            close();
        }
        return num;
    }
}
